<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.notify.notifyConfig.audio_path')"
                        type="file"
                        v-model="baTable.form.items!.audio_path"
                        prop="audio_path"
                        :input-attr="{
                            hideSelectFile: true,
                            showFileList: false,
                        }"
                    >
                        <template #tip v-if="baTable.form.items!.audio_path">
                            <div>
                                <Icon name="fa fa-volume-up" @click="onPlayAudio(baTable.form.items!.audio_path)" style="cursor: pointer" />
                            </div>
                        </template>
                    </FormItem>
                    <FormItem
                        :label="t('shop.notify.notifyConfig.receiver_rule')"
                        type="radio"
                        v-model="baTable.form.items!.receiver_rule"
                        prop="receiver_rule"
                        :input-attr="{
                            content: {
                                0: t('shop.notify.notifyConfig.receiver_rule 0'),
                                1: t('shop.notify.notifyConfig.receiver_rule 1'),
                            },
                            border: true,
                        }"
                    />
                    <FormItem
                        :label="t('shop.notify.notifyConfig.receiver_ids')"
                        type="remoteSelects"
                        v-model="baTable.form.items!.receiver_ids"
                        prop="receiver_ids"
                        :input-attr="{
                            pk: 'ba_admin.id',
                            field: 'username',
                            remoteUrl: adminBaseRoutePath + '/shop.notify.NotifyConfig/getAdminList',
                        }"
                        :placeholder="t('Please select field', { field: t('shop.notify.notifyConfig.receiver_ids') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
import { onPlayAudio } from './index'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    receiver_ids: [buildValidatorData({ name: 'required', title: t('shop.notify.notifyConfig.receiver_ids') })],
})

watch(
    () => baTable.form.items!.receiver_rule,
    (val) => {
        if (val == 1) {
            rules.receiver_ids = [buildValidatorData({ name: 'required', title: t('shop.notify.notifyConfig.receiver_ids') })]
        } else {
            formRef.value?.clearValidate('receiver_ids')
            rules.receiver_ids = []
        }
    }
)
</script>

<style scoped lang="scss"></style>
